$(function() {
   var professionalStuent_datagrid =  $("#professionalStuent_datagrid");
   var professionalStuent_dialog =  $("#professionalStuent_dialog");
   var professionalStuent_form =  $("#professionalStuent_form");
    var leaveStudent_dialog =  $("#leaveStudent_dialog");
    var leaveStudent_form =  $("#leaveStudent_form");
    var transferRecord_dialog =  $("#transferRecord_dialog");
    var transferRecord_form =  $("#transferRecord_form");
    //begin列表信息
    professionalStuent_datagrid.datagrid({
        url: "/professionalStuent/list",
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "id", title: "编号", width: 100,hidden:true},
            {field: "name", title: "学员户名", width: 100},
            {field: "age", title: "年龄", width: 100},
            {field:'marketer',title:'销售',width:100 ,formatter:function (value,row,index) {
                return value?value.username :'未选择销售人员'
            }},
            {field:'tuition1',title:'总学费',width:100 ,formatter:function (value,row,index) {
                if(row.tuition){
                    return row.tuition.originaltuition;
                }
            }},
            {field:'tuition2',title:'已缴学费',width:100 ,formatter:function (value,row,index) {
                if(row.tuition){
                    return row.tuition.paidupcapital;
                }
            }},
            {field:'tuition3',title:'未缴学费',width:100 ,formatter:function (value,row,index) {
                if(row.tuition){
                    return row.tuition.unpaidaltuition;
                }
            }},
           /* {field:'tuition',title:'缴费状态',width:100 ,formatter:function (value,row,index) {
                if(row.tuition.unpaidaltuition){
                    return '<font color="red">未缴清</font>';
                }else{
                    return '<font color="green">已缴清</font>';
                }
            }},*/
            {field: "positiveDate", title: "入学时间", width: 100},
            {field: "school", title: "学校", width: 100},
            {field: "tell", title: "电话", width: 100},
            {field: "email", title: "邮箱", width: 100},
            {field: "payment", title: "支付方式", width: 100 ,formatter:function (value,row,index) {
                return value?value.name :''
            }},

            {field:'interestClass',title:'班级',width:100 ,formatter:function (value,row,index) {
                return value?value.name :''
            }},
            {field: "state", title: "学员转态", width: 100,formatter:function (value,row,index) {
               if(value == 1){
                   return '<font color="green">在读</font>';
               }else if(value == 2){
                   return '<font color="blue">转班中</font>';
               }else if(value == 3){
                   return '<font color="yellow">休学</font>';
               }else if(value == 0){
                   return  '<font color="red">流失</font>';
               }
            }}
        ]],
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pageNumber:1,
        pageSize:10,
        pageList:[3,5,10,20],
        toolbar: "#professionalStuent_tb"
    });
    //end列表信息

    //begin添加/编辑对话框
    professionalStuent_dialog.dialog({
        width: 900,
        height: 400,
        buttons: "#professionalStuent_bb",
        closed: true
    });
    //end添加/编辑对话框

    //begin流失学员对话框
    leaveStudent_dialog.dialog({
        width: 500,
        height: 500,
        buttons: "#leaveStudent_bb",
        closed: true,
        draggable:true
    });
    //end流失学员对话框

    //begin学员转班对话框
    transferRecord_dialog.dialog({
        width: 400,
        height: 400,
        buttons: "#transferRecord_bb",
        closed: true,
        draggable:true
    });
    //end学员转班对话框

    //begin处理功能方法的对象
    var obj = {


        //begin编辑按钮
        edit: function () {
            professionalStuent_form.form("clear");
            professionalStuent_dialog.dialog("setTitle", "正式学员编辑");
            var row = professionalStuent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            if(row){

             row["payment.id"] = row.payment.id;
             row["interestClass.id"] = row.interestClass.id;
             row["schoolCustomer.id"] = row.schoolCustomer.id;
             row["education.id"] = row.education.id;
             row["customerSource.id"] = row.customerSource.id;
             row["marketer.id"] = row.marketer.id;
             row["interestCampu.id"] = row.interestCampu.id;
             row["intentionjob.id"] = row.intentionjob.id;
            }
            professionalStuent_form.form("load", row);
            professionalStuent_dialog.dialog("open");
        },
        //end编辑按钮

        //begin查看按钮
        watch: function () {
            //$("#professionalStuent_bb").hide();
            professionalStuent_form.form("clear");
            professionalStuent_dialog.dialog("setTitle", "帅哥编辑");
            var row = professionalStuent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }

            $.get("/professionalStuent/selectOne?id="+row.id,function(date){
                $("#schoolCustomerId").combobox('setValue', date.schoolCustomer.id);
                $("#educationId").combobox('setValue', date.education.name);
                $("#customerSourceId").combobox('setValue', date.customerSource.name);
                $("#interestClassId").combobox('setValue', date.interestClass.name);
                $("#paymentId").combobox('setValue', date.payment.name);
                $("#marketerId").combobox('setValue', date.marketer.username);
                $("#interestCampuId").combobox('setValue', date.interestCampu.name);
                $("#intentionjobId").combobox('setValue', date.intentionjob.name);




            })

            professionalStuent_form.form("load", row);
            professionalStuent_dialog.dialog("open");
            professionalStuent_form.find("input").attr("readonly",true);
        },
        //end查看按钮



        //begin取消按钮
        cancel: function () {
            professionalStuent_dialog.dialog("close");
        },
        //end取消按钮

        //begin保存按钮
        save: function () {
            var personId = $("#professionalStuentId").val();
            var url = "/professionalStuent/update";

            professionalStuent_form.form("submit", {
                url: url,
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        professionalStuent_dialog.dialog("close");
                        professionalStuent_datagrid.datagrid("reload");
                    }
                }
            });
        },
        //end保存按钮


        // 学员高级查询操作
        query: function() {
            var keywords= $("#keywords").textbox("getValue");
            var beginDate= $("#beginDate").textbox("getValue");
            var endDate= $("#endDate").textbox("getValue");
            professionalStuent_datagrid.datagrid("load",{
                keywords:keywords,
                beginDate:beginDate,
                endDate:endDate
            });
        },
        // 学员高级查询操作 end

        //begin流失学员按钮
        leave: function () {
            var row =professionalStuent_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('亲','请选择要流失的数据');
                return;
            }
            $.messager.confirm('亲','您确认想要放弃这个学生吗？',function(r){
                if (r){
                    //清除数据
                    leaveStudent_dialog.form("clear");
                    if(row){
                        row["professionalStudent.id"] = row.id;
                        row["professionalStudent.name"] = row.name;
                        row["grade.id"] = row.interestClass.id;
                    }
                    // 打开窗口
                    leaveStudent_dialog.dialog('open');
                    //设置文本
                    leaveStudent_dialog.dialog("setTitle",'流失学员信息表');
                    //数据回显
                    leaveStudent_form.form("load",row);

                }
            });


        },
        //end流失学员按钮

        //begin流失学员保存按钮
        save_leave: function () {
            var url = "/leaveStudent/save";
            leaveStudent_form.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        leaveStudent_dialog.dialog("close");
                        professionalStuent_datagrid.datagrid("reload");
                    }
                }
            });
        },
        //end流失学员保存按钮

        //begin流失学员取消按钮
        cancel_leave: function () {
            leaveStudent_dialog.dialog("close");
        },
        //end流失学员取消按钮




        //begin学员转班按钮
        transfer: function () {
            transferRecord_form.form("clear");
            transferRecord_dialog.dialog("setTitle", "学员转班");
            var row = professionalStuent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $.messager.confirm('亲','您确认想要转班吗？',function(r){
                if (r){
                    transferRecord_form.form("clear");
                    if(row){
                        row["professionalStudent.id"] = row.id;
                        row["professionalStudent.name"] = row.name;
                        row["oldgrade.id"] = row.interestClass.id;
                    }
                    // 打开窗口
                    transferRecord_dialog.dialog('open');
                    //设置文本
                    transferRecord_dialog.dialog("setTitle",'流失学员信息表');
                    //数据回显
                    transferRecord_form.form("load",row);
                }
            });

        },
        //end学员转班按钮


        //begin学员转班保存按钮
        save_transferRecord: function () {
            var url = "/transferRecord/save";
            transferRecord_form.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        transferRecord_dialog.dialog("close");
                        professionalStuent_datagrid.datagrid("reload");
                    }
                }
            });
        },
        //end学员转班保存按钮

        //begin学员转班取消按钮
        cancel_transferRecord: function () {
            transferRecord_dialog.dialog("close");
        },
        //end学员转班取消按钮

        //begin复学按钮
        goBack:function (){
            var row = professionalStuent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $.messager.confirm('亲','您确认要复学吗？',function(r){
                if (r){
                    var url = "/professionalStuent/goBack?id="+row.id;
                    $.get(url, function(data){

                        if (!data.success) {
                            $.messager.alert('温馨提示', data.msg);
                        } else {
                            $.messager.alert('温馨提示', "操作成功!");

                            professionalStuent_datagrid.datagrid("reload");
                        }
                    });
                }
            });

        },
        //end复学按钮

        //begin休学按钮
        ouit:function (){
            var row = professionalStuent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $.messager.confirm('亲','您确认要休学吗？',function(r){
                if (r){
                    var url = "/professionalStuent/ouit?id="+row.id;
                    $.get(url, function(data){

                        if (!data.success) {
                            $.messager.alert('温馨提示', data.msg);
                        } else {
                            $.messager.alert('温馨提示', "操作成功!");

                            professionalStuent_datagrid.datagrid("reload");
                        }
                    });
                }
            });

        },
        //end休学按钮


        //begin刷新按钮
        reload:function (){
            professionalStuent_datagrid.datagrid("reload")
        }
        //end刷新按钮


    };
    //end处理功能方法的对象

    //begin处理功能方法对象,实际调用方法
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        obj[cmd]();
    });
    //end处理功能方法对象,实际调用方法

    //点击事件
    professionalStuent_datagrid.datagrid({
        onClickRow: function (index, row) {
           if(row.state == 1){
               $("#transfer").show();
               $("#suspend").show();
               $("#goBack").show();
               $("#leave").show();

                $("#goBack").hide();

           }else if(row.state == 0){

               $("#transfer").hide();
               $("#suspend").hide();
               $("#goBack").hide();
               $("#leave").hide();

           }else if(row.state == 2){

               $("#transfer").show();
               $("#suspend").show();
               $("#goBack").show();
               $("#leave").show();

               $("#transfer").hide();

           }else if(row.state == 3){

               $("#transfer").show();
               $("#suspend").show();
               $("#goBack").show();
               $("#leave").show();

               $("#suspend").hide();
           }
        }
    })

});

